package com.cong.security.controller;

//import com.cong.security.app.social.AppSignUpUtils;
import com.cong.security.config.LoginUser;
import com.cong.security.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description TODO
 * @Author single-聪
 * @Date 2020/1/18 23:27
 * @Version 1.0.1
 **/
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private ProviderSignInUtils providerSignInUtils;

//    @Autowired
//    private AppSignUpUtils appSignUpUtils;

    @RequestMapping("bind")
    public String bind(HttpServletRequest request) {
        log.info("执行用户绑定操作,获取用户手机号即短信校验码进行校验");
        String phone = request.getParameter("phone");// 手机号
        String code = request.getParameter("code");// 验证码
        // 验验证码(隐式注册,只要验证码正确就会返回唯一用户标识)
        String userId = accountService.testSms(phone, code);
        // TODO 将系统userId和第三方账号进行绑定
        providerSignInUtils.doPostSignUp(userId, new ServletWebRequest(request));
        return userId;
    }

//    @RequestMapping("register")
//    public String register(HttpServletRequest request) {
//        log.info("执行用户绑定操作,获取用户手机号即短信校验码进行校验");
//        String phone = request.getParameter("phone");// 手机号
//        String code = request.getParameter("code");// 验证码
//        // 验验证码(隐式注册,只要验证码正确就会返回唯一用户标识)
//        String userId = accountService.testSms(phone, code);
//        // TODO 将系统userId和第三方账号进行绑定
//        appSignUpUtils.doPostSignUp(new ServletWebRequest(request), userId);
//        return userId;
//    }

    @RequestMapping("me")
    public Object me(@AuthenticationPrincipal UserDetails user) {
        return user;
    }

    @RequestMapping("id")
    public String id() {
        return LoginUser.getUserId();
    }
}
